package com.cqgc.controller;

import com.cqgc.pojo.dto.CategoryDTO.CategoryDTO;
import com.cqgc.pojo.dto.CategoryDTO.CategoryPageQueryDTO;
import com.cqgc.pojo.dto.CategoryDTO.CategoryUpdateDTO;
import com.cqgc.pojo.entity.Category;
import com.cqgc.pojo.entity.PageResult;
import com.cqgc.pojo.entity.Result;
import com.cqgc.service.CategoryService;
import com.cqgc.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Delete;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Api(tags = "分类接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */

    @PostMapping("/add")
    @ApiOperation("新增分类")
    @RequiresRoles("admin")
    public Result add(@RequestBody CategoryDTO categoryDTO) {
        if(categoryDTO==null||categoryDTO.getCategoryName()==null|| categoryDTO.getCategoryName().isEmpty()){
            return Result.error("分类名称不能为空");
        }
        categoryService.add(categoryDTO);
        return Result.success("新增成功");
    }

    //删除分类
    @DeleteMapping
    @ApiOperation("删除分类")
    @RequiresRoles("admin")
    public Result detele( Integer cid) {
        int count=categoryService.delete(cid);
        if(count!=0){
            return Result.success("删除成功");
          }
          else{
            return Result.error("删除失败");
          }
    }

    @PutMapping("/update")
    @ApiOperation("修改分类")
    @RequiresRoles("admin")
    public Result update(@RequestBody CategoryUpdateDTO categoryUpdateDTO) {
        categoryService.update(categoryUpdateDTO);
        return Result.success("修改成功");
    }

    @GetMapping("/findAll")
    @ApiOperation("查询所有分类")
    public Result findAll(){
        List<Category> list= categoryService.findAll();
        return Result.success(list);
    }
}
